package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.db.OrgUserDO;
import com.wisewin.circle.service.ActivityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/common")
public class CommonController extends BaseController {

    @Autowired
    private ActivityService activityService;

    @PostMapping("/uploadImageUrl")
    public ResultDTO uploadImageUrl (HttpServletRequest request, MultipartFile[] lstFile, String fileType) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        Integer userId = null;
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            userId = loginUser.getId();
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            userId = orgUserDO.getCreateUserId();
        }
        return activityService.uploadFile(lstFile, userId, fileType);
    }
}
